package com.minhua.jijiehao2.mainframe.circle.businessDetailActivity.impl

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.BusinessBean
import com.minhua.jijiehao2.bean.ContactsBean
import com.minhua.jijiehao2.mainframe.circle.businessDetailActivity.imp.IBusinessDetailView
import com.minhua.jijiehao2.utils.CountUtils
import kotlinx.android.synthetic.main.activity_business_detail.*

class BusinessDetailActivity : BaseActivity<IBusinessDetailView, BusinessDetailPresenter>(), IBusinessDetailView {
    override fun getLayoutId(): Int = R.layout.activity_business_detail

    override fun getPresenter(): BusinessDetailPresenter = BusinessDetailPresenter()

    companion object {
        fun intentStart(context: Context, businessBean: BusinessBean) {
            val intent = Intent(context, BusinessDetailActivity::class.java)
            intent.putExtra("businessBean", businessBean)
            context.startActivity(intent)
        }
    }

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            else -> {
            }
        }
    }

    private val listContacts = arrayListOf<ContactsBean>()
    private var contactsAdapter: BaseCommonAdapter<ContactsBean>? = null

    override fun initView() {
        contactsAdapter = object : BaseCommonAdapter<ContactsBean>(this, R.layout.item_business_contacts, listContacts) {
            override fun convert(holder: ViewHolder?, t: ContactsBean?) {
                holder?.setText(R.id.tvName, t?.name ?: "")
                holder?.setText(R.id.tvJob, t?.job ?: "")
                holder?.setText(R.id.tvMajor, t?.module ?: "")
                holder?.setText(R.id.tvQq, t?.qq ?: "")
                holder?.setText(R.id.tvWeChat, t?.wx ?: "")
                holder?.setText(R.id.tvPhone, t?.tel ?: "")
            }
        }
        rvContacts.isNestedScrollingEnabled = false
        rvContacts.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContacts.adapter = contactsAdapter
    }

    override fun initData() {
        val businessBean = intent.getSerializableExtra("businessBean") as BusinessBean
        CountUtils.deitailClick(businessBean.proc_id)
        setOtherInfo(businessBean)
        listContacts.clear()
        listContacts.addAll(businessBean.operativeInfoList!!)
        contactsAdapter?.notifyDataSetChanged()
    }

    private fun setOtherInfo(businessBean: BusinessBean) {
        Glide.with(this).load(businessBean.logo_path).into(ivCompanyIcon)
        tvName.text = businessBean.proc_name
        llInfo.text = "公司网址:${businessBean.official_url}"
        tvAddress.text = "公司地址:${businessBean.companyAddress}"
        val message = businessBean.introduction ?: ""
        if (message.toLowerCase().startsWith("<html")) {
            wvWeb.visibility = View.VISIBLE
            tvMessage.visibility = View.GONE
            wvWeb.loadDataWithBaseURL(null, message, "text/html", "utf-8", null)
        } else {
            wvWeb.visibility = View.GONE
            tvMessage.visibility = View.VISIBLE
            tvMessage.text = message
        }
    }
}
